﻿using System;
using System.Web.UI.WebControls;
using Core.Library;
using DataObject.Article;
using DataObject.Category;

namespace KMT.Controls.Article
{
    public partial class SubFooterArticle : System.Web.UI.UserControl
    {
        public int CategoryId { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            ListArticleByCategory();
        }

        private void ListArticleByCategory()
        {
            var lstArticle = ArticleController.Article_GetListByCategoryId(CategoryId);
            if (lstArticle.Count > 0)
            {
                rptArticle.DataSource = lstArticle;
                rptArticle.DataBind();
            }
        }

        protected void rptArticle_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var ltArticle  = (Literal)e.Item.FindControl("ltArticle");
                var articleId  = ConvertUtility.ToInt32(((HiddenField)e.Item.FindControl("hfArticleId")).Value);
                var categoryId = ConvertUtility.ToInt32(((HiddenField)e.Item.FindControl("hfCategoryId")).Value);
                var title      = ((HiddenField)e.Item.FindControl("hfTitle")).Value;                
                
                ltArticle.Text = string.Format("<li><a href=\"{0}\">{1}</a></li>",
                    UrlHelpers.ArticleDetail(CategoryController.GetCategoryName(categoryId), articleId, title), title);
            }
        }
    }
}